<?php
declare (strict_types=1);

namespace app\circle\model;

use app\common\logic\Attach as AttachLogic;
use app\common\model\{Attach, BaseModel};
use app\user\model\user\{Account as User, Follow, VipUser};
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasOne;

/**
 * 活动模型
 */
class Circle extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'circle';

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'           => 'integer',
        'user_id'      => 'integer',
        'type'         => 'integer',
        'attach'       => 'json',
        'recircle_id'  => 'integer',
        'recircle_num' => 'integer',
        'comment_num'  => 'integer',
        'like_num'     => 'integer',
        'create_time'  => 'integer',
    ];

    protected $append = ['attach_url'];
    protected $hidden = ['delete_time'];
    protected $globalScope = ['mhm'];

    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     */
    public static function onBeforeInsert($model)
    {
        $model->mhm_id = $model->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 附件url获取器
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-14
     *
     * @param $value
     * @param $data
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAttachUrlAttr($value = '', $data): array
    {
        $url = [];
        if (!isset($data['attach']) || !$data['attach']) return $url;
        $attach = json_decode($data['attach'], true);
        if (!count($attach)) return $url;
        if (is_array($attach)) {
            foreach ($attach as $id) {
                $attach = Attach::find($id);
                if ($attach) {
                    if (app(AttachLogic::class)->isAudioOrVideo($attach)) {
                        $urls  = array_column($attach->fileurl, null, 'definition');
                        $url[] = $urls['OD']['play_url'];
                    } else {
                        $url[] = $attach->fileurl ?? '';
                    }
                } else {
                    $url[] = '';
                }
            }
        }

        return $url;
    }

    /**
     * 关联关注
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-31
     */
    public function hasFollowThisPerson(): HasOne
    {
        return $this->hasOne(Follow::class, 'follow_user_id', 'user_id')->where('user_id', request()->mid);
    }

    /**
     * 关联点赞
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-31
     */
    public function like()
    {
        return $this->hasOne(Like::class, 'obj_id')->where('type', 1)->whereUserId(request()->mid);
    }

    /**
     * 关联用户头像和昵称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-17
     */
    public function userBase(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id')->with(['avatar_url'])->bind(['nick_name', 'avatar_url']);
    }

    /**
     * 关联原著
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-21
     */
    public function orignal(): BelongsTo
    {
        return $this->belongsTo(Circle::class, 'orignal_id', 'id')->with(['user_base'])->bind([
            'orignal_user'    => 'nick_name',
            'orignal_content' => 'content',
            'orignal_attach'  => 'attach_url',
        ]);
    }

    /**
     * 关联用户是否为会员
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-09-22
     */
    public function userVip(): HasOne
    {
        return $this->hasOne(VipUser::class, 'user_id', 'user_id')->where('expire_time', '>', time());
    }

}
